//
//  KTDate.m
//
//
//  Created by xu on 8/16/14.
//  Copyright (c) 2014 . All rights reserved.
//

#import "KTDate.h"

@implementation KTDate


- (id)copyWithZone:(NSZone *)zone
{
    KTDate *date = [KTDate allocWithZone:zone];
    date.date = [self.date copy];
    
    return date;
}

- (instancetype)init
{
    if (self = [super init]) {
        self.date = [NSDate date];
    }
    
    return self;
}

+ (instancetype)dateWithDate:(NSDate *)date
{
    KTDate *hnDate = [KTDate new];
    hnDate.date = date;
    
    return hnDate;
}

+ (instancetype)dateWithCurrentyyyyMMddDate
{
    KTDate *date = [KTDate new];
    
    NSString *currentDateStr = [date dateStringWithFormatyyyyMMdd];
    
    date = [KTDate dateWithDoBestYYYYMMDDDateString:currentDateStr];
    
    return date;
}

+ (NSString *)dateWithCurrentyyyyMMddHHmmssString
{
    KTDate *date = [KTDate new];
    
    NSString *currentDateStr = [date dateStringWithFormatyyyyMMddHHmmss];
    
    return currentDateStr;
}

+ (instancetype)dateWithDateString:(NSString *)string dateFormat:(NSString *)dateFormat
{
    if (dateFormat.length == 0) {
        dateFormat = @"yyyy-MM-dd HH:mm:ss";
    }
    
    KTDate *hnDate = [KTDate new];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:dateFormat];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    NSDate *date = [dateFormatter dateFromString:string];
    
    if (date == nil) {
        //date = [NSDate date];
    }
    
    hnDate.date = date;
    
    return hnDate;
}

+ (instancetype)dateWithDoBestYYYYMMDDDateString:(NSString *)string
{
    NSArray *dateFormatArray = @[@"yyyyMMdd",@"yyyyMMddHHmmss",@"yyyyMMddHHmm",@"yyyyMMddHH"];
    
    string = [string stringByReplacingOccurrencesOfString:@"-" withString:@""];
    string = [string stringByReplacingOccurrencesOfString:@":" withString:@""];
    string = [string stringByReplacingOccurrencesOfString:@"." withString:@""];
    string = [string stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    KTDate *hnDate = [KTDate new];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    
    for (NSString *dateFormat in dateFormatArray) {
        [dateFormatter setDateFormat:dateFormat];
        
        NSDate *date = [dateFormatter dateFromString:string];
        
        if (date) {
            hnDate.date = date;
            break;
        }
    }
    
    return hnDate;
}

//删除 年月日
- (void)removeDate
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"hh:mm:ss"];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    NSString *myDateStr = [dateFormatter stringFromDate:_date];
    NSDate *newDate = [dateFormatter dateFromString:myDateStr];
    
    self.date = newDate;
}

//删除 时分秒
- (void)removeTime
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
    NSString *myDateStr = [dateFormatter stringFromDate:_date];
    NSDate *newDate = [dateFormatter dateFromString:myDateStr];
    
    self.date = newDate;
}

- (NSTimeInterval)timeIntervalSinceNow
{
    return _date.timeIntervalSinceNow;
}

- (NSTimeInterval)timeIntervalSince1970
{
    return _date.timeIntervalSince1970;
}

- (void)dealloc
{
    self.date = nil;
}

- (NSString *)hourAndMinitue
{
    return [self dateStringWithDateFormat:@"HH:mm"];
}

- (NSString *)yearMonthDay
{
    return [self dateStringWithDateFormat:@"yyyy年MM月dd日"];
}

- (NSString *)dateStringWithFormatMMdd
{
    return [self dateStringWithDateFormat:@"MM-dd"];
}

- (NSString *)dateStringWithDateFormat:(NSString *)dateFormat
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:dateFormat];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    NSString *str = [dateFormatter stringFromDate:self.date];
    
    return str ? str : @"";
}



- (NSString *)dateStringWithFormatyyyyMMddHHmmss
{
    return [self dateStringWithDateFormat:@"yyyy-MM-dd HH:mm:ss"];
}

- (NSString *)dateStringWithFormatMMddHHmm
{
    return [self dateStringWithDateFormat:@"MM-dd HH:mm"];
}

- (NSString *)dateStringWithFormatyyyyMMdd
{//(切勿修改格式,很多请求参数用到)
    return [self dateStringWithDateFormat:@"yyyy-MM-dd"];
}

- (NSTimeInterval)timeIntervalSinceDate:(KTDate *)anotherDate ignoreDate:(BOOL)ignoreDate
{
    if (ignoreDate) {
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"hh:mm:ss a"];
        [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]];
        NSString *myDateStr = [dateFormatter stringFromDate:_date];
        NSDate *myDate = [dateFormatter dateFromString:myDateStr];
        
        NSString *otherDateStr = [dateFormatter stringFromDate:anotherDate.date];
        NSDate *otherDate = [dateFormatter dateFromString:otherDateStr];
        
        return [myDate timeIntervalSinceDate:otherDate];
        
    }
    else {
        return [_date timeIntervalSinceDate:anotherDate.date];
    }
}

- (NSTimeInterval)timeIntervalIgnoreTimeSinceDate:(KTDate *)anotherDate
{
    NSString *format = @"yyyyMMdd";
    KTDate *myDate = [KTDate dateWithDateString:[self dateStringWithDateFormat:format] dateFormat:format];
    KTDate *toDate = [KTDate dateWithDateString:[anotherDate dateStringWithDateFormat:format] dateFormat:format];
    
    return myDate.date.timeIntervalSince1970 - toDate.date.timeIntervalSince1970;
    
}

- (NSTimeInterval)timeIntervalNotIgnoreTimeSinceDate:(KTDate *)anotherDate//比较年月日 时分秒
{
    return self.date.timeIntervalSince1970 - anotherDate.date.timeIntervalSince1970;
}

- (NSString *)timeIntervalDescriptionSinceDate:(KTDate *)anotherDate
{
    NSTimeInterval timeInterval = [self timeIntervalSinceDate:anotherDate ignoreDate:NO];
    
    
    NSString *description = @"";
    
    CGFloat baseTimeMax = CGFLOAT_MAX;
    CGFloat baseTimeMin = 60*60*24*365;
    int num = timeInterval/baseTimeMin;
    if (fabs(timeInterval) >= baseTimeMin && fabs(timeInterval) < baseTimeMax) {
        description = [description stringByAppendingFormat:@"%d年",num];
        timeInterval -= num*baseTimeMin;
    }
    
    baseTimeMax = baseTimeMin;
    baseTimeMin = 60*60*24;
    num = timeInterval/baseTimeMin;
    if (fabs(timeInterval) >= baseTimeMin && fabs(timeInterval) < baseTimeMax) {
        description = [description stringByAppendingFormat:@"%d天",num];
        timeInterval -= num*baseTimeMin;
    }
    
    baseTimeMax = baseTimeMin;
    baseTimeMin = 60*60;
    num = timeInterval/baseTimeMin;
    if (fabs(timeInterval) >= baseTimeMin && fabs(timeInterval) < baseTimeMax) {
        description = [description stringByAppendingFormat:@"%d时",num];
        timeInterval -= num*baseTimeMin;
    }
    
    baseTimeMax = baseTimeMin;
    baseTimeMin = 60;
    num = timeInterval/baseTimeMin;
    if (fabs(timeInterval) >= baseTimeMin && fabs(timeInterval) < baseTimeMax) {
        description = [description stringByAppendingFormat:@"%d分",num];
        timeInterval -= num*baseTimeMin;
    }
    
    baseTimeMax = baseTimeMin;
    baseTimeMin = 0;
    if (fabs(timeInterval) >= baseTimeMin && fabs(timeInterval) < baseTimeMax) {
        description = [description stringByAppendingFormat:@"%.0f秒",timeInterval];
    }
    
    
    return description;
}

- (NSInteger)daysSinceDate:(KTDate *)startDate
{
    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSDateComponents *components = [gregorian components:NSCalendarUnitDay fromDate:startDate.date toDate:self.date options:0];
    return [components day];
}

///判断是否是同一天... 忽略时分秒
- (BOOL)isSameDayWithDate:(KTDate *)anotherDate
{
    KTDate *oldDay = [KTDate dateWithDate:self.date.copy];
    KTDate *newDay = [KTDate dateWithDate:anotherDate.date.copy];
    
    [oldDay removeTime];
    [newDay removeTime];
    
    return fabs(oldDay.date.timeIntervalSince1970 - newDay.date.timeIntervalSince1970) < CGFLOAT_MIN;
}

- (NSString *)weekday
{
    return [KTDate weekdayWithDate:_date];
}

- (NSString *)weekday2
{
    return [[self weekday] stringByReplacingOccurrencesOfString:@"周" withString:@"星期"];
}

+ (NSString *)weekdayWithDate:(NSDate *)date
{
    NSString *weekstr = @"";
    
    if (date) {
        NSDateComponents *componets = [[NSCalendar autoupdatingCurrentCalendar] components:NSCalendarUnitWeekday fromDate:date];
        switch ([componets weekday]) {
            case 1:
                weekstr = @"周日";
                break;
                
            case 2:
                weekstr = @"周一";
                break;
                
            case 3:
                weekstr = @"周二";
                break;
                
            case 4:
                weekstr = @"周三";
                break;
                
            case 5:
                weekstr = @"周四";
                break;
                
            case 6:
                weekstr = @"周五";
                break;
                
            case 7:
                weekstr = @"周六";
                break;
                
            default:
                break;
        }
    }
    
    return weekstr;
}

- (NSString *)weekdayAndDate
{
    return [[self dateStringWithDateFormat:@"yyy-MM-dd"] stringByAppendingString:[self weekday]];
}

+ (NSString *)weekdayAndDateWithDate:(NSDate *)date
{
    KTDate *myDate = [KTDate dateWithDate:date];
    return [myDate weekday];
}

+ (NSString *)currentDateTime
{
    NSString *str = [[KTDate new] dateStringWithDateFormat:@"yyyyMMddHHmmssSSS"];
    
    return str;
}

- (instancetype)previousDays:(NSUInteger)days
{
    NSDate *date = [_date dateByAddingTimeInterval:-24*60*60.*days];
    return [KTDate dateWithDate:date];
}

- (instancetype)nextDays:(NSUInteger)days
{
    NSDate *date = [_date dateByAddingTimeInterval:24*60*60.*days];
    return [KTDate dateWithDate:date];
}

- (NSString *)description
{
    return [_date description];
}


@end
